package com.bigpang.framework.example.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author YongQiang
 * @Date 2021/7/13 15:49
 * @Version 1.0
 */
public class MyBeanFactory {
    public static UserService createUserService(){
        final UserService userService = new UserServiceImpl();
        final MyAspect myAspect = new MyAspect();

        UserService userService1 = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                myAspect.before();
                Object object = method.invoke(userService, args);
                myAspect.after();

                return object;
            }
        });
        return userService1;
    }
}
